/*
 * #depends subsonic-musicstore.js
 */
Ext.regModel('Ext.ux.mattgoldspink.subsonic.model.Music', {
    idProperty: 'id',
    fields: Ext.ux.mattgoldspink.subsonic.Fields
});

Ext.ux.mattgoldspink.subsonic.stores.MusicStore = Ext.extend(Ext.data.Store, {
   constructor: function(config) {
       var params = {};
       Ext.apply(params, Ext.ux.mattgoldspink.subsonic.LoginDetails);
       Ext.apply(config, {
            model: 'Ext.ux.mattgoldspink.subsonic.model.Music',
            id:'Ext.ux.mattgoldspink.subsonic.stores.MusicStore',
            sorters: 'name',
            getGroupString : function(record) {
				if (record.get('isDir')) {
					var name = record.get('name');
					if (name.length > 0) {
						return record.get('name')[0].toUpperCase();
					}
				}
				return '';
            },
            proxy: {
                type: 'ajax',
                url: Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getIndexes.view',
                extraParams: params,
                requestMethod: 'GET',
                reader: this.makeIndexesReader()
            }
        });
        Ext.ux.mattgoldspink.subsonic.stores.MusicStore.superclass.constructor.call(this, config);
        this.addAfterLoadListeners();
    },
    addAfterLoadListeners: function(){
        this.on('load', this.handleLoad, this);
    },
    handleLoad: function(store, records, successful) {
        Ext.Msg.hide();
        if (this.getProxy().url === Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getIndexes.view') {
            this.setupMusicDirectoryCall();
        }
    },
    loadNextData: function(id) {
        var rec = this.getById(id), typeOfRequest = rec.get('typeOfRequest'), idPropName = 'id';
        if (typeOfRequest === 'musicDirectory' || typeOfRequest === 'indexes') {
            this.setupMusicDirectoryCall();
        } else if (typeOfRequest === 'musicFolder') {
			this.setupIndexesCall();
			idPropName = 'musicFolderId';
		}
        if (rec.get('suffix') !== 'mp3') {
            this.getProxy().extraParams[idPropName]=id;
            this.clearFilter();
            this.load();
        }
    },
    setupIndexesCall: function(){
        this.getProxy().url = Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getIndexes.view';
        this.getProxy().setReader(this.makeIndexesReader());
    },
    setupMusicDirectoryCall: function(){
        this.getProxy().url = Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getMusicDirectory.view';
        this.getProxy().setReader(this.makeMusicDirectoryReader());
    },
	setupPlaylistCall: function(){
        this.getProxy().url = Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getPlaylist.view';
        this.getProxy().setReader(this.makePlaylistReader());
    },
    makePlaylistReader: function() {
        if (!Ext.isDefined(this.playlistReader)) {
            this.playlistReader = new Ext.data.JsonReader({
                root: 'playlist.entry',
                getData: function(data){
                    var recs;
                    data = data['subsonic-response'];
                    recs = data.playlist.entry;
                    if (!Ext.isArray(recs)) {
                        data.playlist.entry = [recs];
                    }
                    Ext.each(recs, function(rec){
                        rec.typeOfRequest = 'playlist';
						rec.name = rec.title;
                    });
                    return data;
                }
            });
        }
        return this.playlistReader;
    },
    makeMusicDirectoryReader: function() {
        if (!Ext.isDefined(this.musicDirectoryReader)) {
            this.musicDirectoryReader = new Ext.data.JsonReader({
                root: 'directory.child',
                getData: function(data){
                    var recs;
                    data = data['subsonic-response'];
                    recs = data.directory.child;
                    if (!Ext.isArray(recs)) {
                        data.directory.child = [recs];
                    }
                    Ext.each(recs, function(rec){
                        if (!rec.name) {
                            rec.name = rec.title;
                            delete rec.title;
                        }
                        rec.typeOfRequest = 'musicDirectory';
                    });
                    return data;
                }
            });
        }
        return this.musicDirectoryReader;
    },
    makeIndexesReader: function() {
        if (!Ext.isDefined(this.indexesReader)) {
            this.indexesReader = new Ext.data.JsonReader({
                root: 'indexes.index',
                getData: function(data){
                    var recs;
                    data = data['subsonic-response'];
                    recs = data.indexes.index;
                    data.indexes.index = [];
                    if (!Ext.isArray(recs)) {
                        recs = [recs];
                    }
                    Ext.each(recs, function(index){
                        Ext.each(index.artist, function(artist) {
                            artist.typeOfRequest = 'indexes';
                            artist.isDir = true;
                            data.indexes.index.push(artist);
                        });
                    });
                    return data;
                }
            });
        }
        return this.indexesReader;
    }
}); 
